<template>
  <el-dialog
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :title="!dataForm.id ? $t('add') : $t('update')"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      @keyup.enter.native="dataFormSubmitHandle()"
      label-width="120px"
      ref="dataForm"
      v-loading="loading"
    >

      <el-form-item
        :label="$t('model.name')"
        prop="name">
        <el-input
          :placeholder="$t('model.name')"
          v-model="dataForm.name"
        />
      </el-form-item>
      <el-form-item
        :label="$t('mike.sn8')"
        prop="sn8">
        <el-input
          :placeholder="$t('mike.sn8')"
          v-model="dataForm.sn8"
        />
      </el-form-item>
      <el-form-item
        :label="$t('mike.sellType')"
        prop="sellType">
        <el-input
          :placeholder="$t('mike.sellType')"
          v-model="dataForm.sellType"
        />
      </el-form-item>

    </el-form>
    <template slot="footer">
      <el-button
        :loading="requesting"
        @click="visible = false">{{
          $t("cancel")
        }}</el-button>
      <el-button
        :loading="requesting"
        @click="dataFormSubmitHandle()"
        type="primary"
      >{{ $t("confirm") }}
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'

import mixinViewModule from '@/mixins/mike.view-module'
export default {
  name: 'Sn8AddOrUpdate',
  mixins: [mixinViewModule],
  data () {
    return {
      visible: false,
      loading: false,
      requesting: false,
      dataForm: {
        createTime: '',
        descri: '',
        ext: '',
        flag: 0,
        id: '',
        name: '',
        sellType: '',
        sn8: '',
        totalCounts: 0
      }
    }
  },
  computed: {
    dataRule () {
      return {
        sn8: [
          {
            required: true,
            message: this.$t('validate.required'),
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: this.$t('validate.required'),
            trigger: 'blur'
          }
        ]

      }
    }
  },
  watch: {
    visible (newValue, oldValue) {
      if (newValue) {
        return
      }
      this.dataForm = {
        createTime: '',
        descri: '',
        ext: '',
        flag: 0,
        id: '',
        name: '',
        sellType: '',
        sn8: '',
        totalCounts: 0
      }
    }
  },
  beforeMount () {

  },
  mounted () {},
  methods: {
    init () {
      this.visible = true

      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },

    // 获取信息
    getInfo () {
      this.loading = true
      this.$http
        .post(`${this.mikeNetworkBase}/midea/menu/sn8/get/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 200) {
            return this.$message.error(res.msg)
          }

          this.dataForm = {
            ...this.dataForm,
            ...res.data
          }
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false
        })
    },

    dataFormSubmitHandle: debounce(
      function () {
        this.$refs['dataForm'].validate(valid => {
          if (!valid) {
            return false
          }
          if (this.requesting) {
            return
          }
          let params = {
            ...this.dataForm
            // status: this.dataForm.status ? 1 : 0
          }

          this.requesting = true
          this.$http.post(
            `${this.mikeNetworkBase}/midea/menu/sn8/save`,
            params
          )
            .then(({ data: res }) => {
              if (res.code !== 200) {
                return this.$message.error(res.msg)
              }
              this.$message({
                message: this.$t('prompt.success'),
                type: 'success',
                duration: 500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
            .catch(() => {})
            .finally(() => {
              this.requesting = false
            })
        })
      },
      1000,
      { leading: true, trailing: false }
    )
  }
}
</script>

<style scoped></style>
